package com.hzlh.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {

	/**
	 * 剩余天数
	 * @param dateTargetStr
	 * @return
	 * @throws ParseException 
	 */
	public static int daysLeft(String dateTargetStr) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date soure = sdf.parse(dateTargetStr);
		Date now = new Date();
		return (int)((soure.getTime() - now.getTime())/(1000*60*60*24));
	}
	public static String dateStr(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		return dateStr;
	}
	public static String dateStr(Date date ,String pattern){
		SimpleDateFormat sdf=new SimpleDateFormat(pattern);
		String dateStr = sdf.format(date);
		return dateStr;
	}
	public static String dataStr(String format,Long time){
		String date = new java.text.SimpleDateFormat(format).
				format(new java.util.Date(time * 1000));
		return date;
	}
	public static String getCurrDate(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(date);
		return dateStr;
	}
	public static Date getFullDate(String str, String pattern) {
	     try {
	         SimpleDateFormat format = new SimpleDateFormat(pattern);
	         Date date = format.parse(str.trim());
	         return date;
	     } catch (Exception ex) {
	         ex.printStackTrace();
	     }
	     return null;
	}
	  /**
     * 日期加减操作
     * @param source 源日期
     * @param num 推迟天数 + 为往后 - 为往前
     * @return
     */
    public static Date dateRoler(Date source, int num) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(source);
            c.add(c.DAY_OF_MONTH, num);
            return c.getTime();
        } catch (Exception e) {
            return null;
        }
    }

}
